package com.users.rn.rncommon.util;

import android.content.Context;
import android.text.TextUtils;
import com.users.rn.common.init.ALHReactNativeInit;
import com.users.rn.rncommon.network.utils.SharePreferenceUtils;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * @Description:
 * @Author: bluce
 * @Date: 2020/12/3 13:52
 */
public class RNDebugUtils {

    public static RNDebugModel deserialize(Context context, String json) {
        if (!ALHReactNativeInit.INSTANCE.getInitInterface().isTestEnv()) {
            return null;
        } else if (json != null && !json.isEmpty()) {
            JSONObject jsonObject = null;
            try {
                jsonObject = new JSONObject(json);
                int status = jsonObject.optInt(ReactConst.DEBUG_STATUS, 1);
                String host = jsonObject.optString(ReactConst.DEBUG_HOST, "");
                String plugin = jsonObject.optString(ReactConst.PLUGINID, "");
                if (!TextUtils.isEmpty(host) && !TextUtils.isEmpty(plugin)) {
                    return new RNDebugModel(plugin, host, status == 0);
                } else {
                    return null;
                }
            } catch (JSONException e) {
                e.printStackTrace();
                return null;
            }
        }
        return null;
    }

    public static void setDebugStatus(Context context, RNDebugModel debugModel) {
        if (context != null && debugModel != null) {
            SharePreferenceUtils.edit(context, "react_native_users_debug")
                    .putString(debugModel.pluginId, debugModel.ipPort + "=" + debugModel.isEnable)
                    .apply();
            ReactUtils.setDebugInfo(context, debugModel);
        }
    }

    public static String getDebugIp(Context context, String pluginId) {
        if (context != null && !TextUtils.isEmpty(pluginId)) {
            try {
                String s = SharePreferenceUtils.getString(context, "react_native_users_debug", pluginId);
                if (TextUtils.isEmpty(s)) {
                    return "";
                }
                int i = s.indexOf("=");
                if (i < 0) {
                    return "";
                }
                String ipPort = s.substring(0, i);
                String enable = s.substring(i + 1, s.length());
                if ("true".equalsIgnoreCase(enable) && !TextUtils.isEmpty(ipPort)) {
                    return ipPort;
                }
            } catch (Exception var6) {
                var6.printStackTrace();
            }
            return "";
        } else {
            return "";
        }
    }

    public static class RNDebugModel {
        public String pluginId;
        public String ipPort;
        public boolean isEnable;

        public RNDebugModel(String pluginId, String ipPort, boolean isEnable) {
            if (!ipPort.contains(":")) {
                ipPort = ipPort + ":8081";
            }
            this.pluginId = pluginId;
            this.ipPort = ipPort;
            this.isEnable = isEnable;
        }
    }
}
